package org.example.erat.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.dto.ResponseDTO;
import org.example.erat.dto.StudentDTO;
import org.example.erat.service.StudentService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 学生控制器
 */
@RestController
@RequestMapping("/students")
@RequiredArgsConstructor
@Slf4j
public class StudentController {

    private final StudentService studentService;

    /**
     * 创建学生
     * @param studentDTO 学生DTO
     * @return 响应
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseDTO<StudentDTO> createStudent(@Valid @RequestBody StudentDTO studentDTO) {
        log.info("创建学生请求: {}", studentDTO);
        StudentDTO createdStudent = studentService.createStudent(studentDTO);
        return ResponseDTO.success("学生创建成功", createdStudent);
    }

    /**
     * 更新学生
     * @param id 学生ID
     * @param studentDTO 学生DTO
     * @return 响应
     */
    @PutMapping("/{id}")
    public ResponseDTO<StudentDTO> updateStudent(@PathVariable Long id, @Valid @RequestBody StudentDTO studentDTO) {
        log.info("更新学生请求: id={}, student={}", id, studentDTO);
        StudentDTO updatedStudent = studentService.updateStudent(id, studentDTO);
        return ResponseDTO.success("学生更新成功", updatedStudent);
    }

    /**
     * 删除学生
     * @param id 学生ID
     * @return 响应
     */
    @DeleteMapping("/{id}")
    public ResponseDTO<Void> deleteStudent(@PathVariable Long id) {
        log.info("删除学生请求: id={}", id);
        studentService.deleteStudent(id);
        return ResponseDTO.success("学生删除成功", null);
    }

    /**
     * 获取学生
     * @param id 学生ID
     * @return 响应
     */
    @GetMapping("/{id}")
    public ResponseDTO<StudentDTO> getStudent(@PathVariable Long id) {
        log.info("获取学生请求: id={}", id);
        StudentDTO student = studentService.getStudent(id);
        return ResponseDTO.success(student);
    }

    /**
     * 获取所有学生
     * @return 响应
     */
    @GetMapping
    public ResponseDTO<List<StudentDTO>> getAllStudents() {
        log.info("获取所有学生请求");
        List<StudentDTO> students = studentService.getAllStudents();
        return ResponseDTO.success(students);
    }

    /**
     * 根据班级ID获取学生列表
     * @param classId 班级ID
     * @return 响应
     */
    @GetMapping("/class/{classId}")
    public ResponseDTO<List<StudentDTO>> getStudentsByClass(@PathVariable Long classId) {
        log.info("获取班级的学生列表请求: classId={}", classId);
        List<StudentDTO> students = studentService.getStudentsByClass(classId);
        return ResponseDTO.success(students);
    }

    /**
     * 从Excel导入学生
     * @param classId 班级ID
     * @param file Excel文件
     * @return 响应
     */
    @PostMapping(value = "/import/{classId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseDTO<List<StudentDTO>> importStudentsFromExcel(
            @PathVariable Long classId,
            @RequestParam("file") MultipartFile file) {
        log.info("导入学生请求: classId={}, fileName={}", classId, file.getOriginalFilename());
        
        if (file.isEmpty()) {
            return ResponseDTO.error(400, "文件不能为空");
        }
        
        String fileName = file.getOriginalFilename();
        if (fileName == null || !(fileName.endsWith(".xlsx") || fileName.endsWith(".xls") || fileName.endsWith(".csv"))) {
            return ResponseDTO.error(400, "只支持Excel文件(.xlsx, .xls)或CSV文件");
        }
        
        List<StudentDTO> importedStudents = studentService.importStudentsFromExcel(classId, file);
        return ResponseDTO.success("成功导入 " + importedStudents.size() + " 名学生", importedStudents);
    }
}
